//	CP2013 Traffic Simulator Assignment
//      Traffic Model Graphical Representation
//	Java version
//      Coded by Matthew Johnson

package traffic;

import javax.swing.*;
import java.awt.*;
import java.util.Random;

//This draws the on screen representation of the intersection
public class GraphicPanel extends JPanel
{

    //A reference to the model to draw
    private Model model;
    //The graphics object responsible for drawing on the panel
    private Graphics g;
    //The width and height of a square
    private int sqWidth, sqHeight;
    //Random number generator for car color
    private Random rNum;

    //Sets the model the Panel needs to draw
    public void setModel(Model m)
    {
        model = m;
    }

    @Override
    //Refreshes the display
    public void paintComponent(Graphics gr){

        g = gr;

        //If no model is set there is nothing to draw
        if (model != null){

            //Fills the Panel with Green
            g.setColor(Color.GREEN);
            g.fillRect(0, 0, getWidth(), getHeight());

            //Setup the square size
            sqWidth = getWidth() / (2 * Lane.DEFAULT_LENGTH + model.settings.nsLanes);
            sqHeight = getHeight() / (2 * Lane.DEFAULT_LENGTH + model.settings.ewLanes);

            //The width and height of each lane
            int laneHeight = 0;
            int laneWidth = 0;

            //Draw each Lane
            g.setColor(Color.GRAY);
            for (Road r:model.roads){
                for (Lane l:r.lanes){
                    switch (l.direction){
                        case NORTH:
                        case SOUTH:
                            laneHeight = l.length;
                            laneWidth = 1;
                            break;
                        case EAST:
                        case WEST:
                            laneHeight = 1;
                            laneWidth = l.length;
                            break;
                    }
                    fillRect(l.anchor.x, l.anchor.y, laneWidth, laneHeight);
                }
            }

            //Draw Intersection
            fillRect(model.intSct.getAnchor().x, model.intSct.getAnchor().y, model.intSct.getWidth(), model.intSct.getHeight());

            //Draw Cars
            //Setup Random number generator
            rNum = new Random();
            //For each vehicle
            for (Vehicle v:model.vehicles){
                //Draw it
                drawCar(v.getColour(), v.getPosition().x, v.getPosition().y, 1, 1);
            }

            //Draw Lights
            //Northbound Light
            g.setColor(Color.BLACK);
            fillRect((Lane.DEFAULT_LENGTH + model.settings.nsLanes), Lane.DEFAULT_LENGTH  + model.settings.ewLanes, 1, 3);
            //The lit light's colour and position depends on the light's state
            switch (model.intSct.getVLight().state){
                case RED:
                    g.setColor(Color.red);
                    drawLight(Lane.DEFAULT_LENGTH + model.settings.nsLanes, Lane.DEFAULT_LENGTH + model.settings.ewLanes);
                    break;
                case YELLOW:
                    g.setColor(Color.YELLOW);
                    drawLight(Lane.DEFAULT_LENGTH + model.settings.nsLanes, Lane.DEFAULT_LENGTH + model.settings.ewLanes + 1);
                    break;
                case GREEN:
                    g.setColor(Color.green);
                    drawLight(Lane.DEFAULT_LENGTH + model.settings.nsLanes, Lane.DEFAULT_LENGTH + model.settings.ewLanes + 2);
                    break;
            }

            //Southbound Light
            g.setColor(Color.black);
            fillRect(Lane.DEFAULT_LENGTH - 3, Lane.DEFAULT_LENGTH + model.settings.ewLanes, 3, 1);
            //The lit light's colour and position depends on the light's state
            switch (model.intSct.getHLight().state){
                case RED:
                    g.setColor(Color.red);
                    drawLight(Lane.DEFAULT_LENGTH - 1, Lane.DEFAULT_LENGTH + model.settings.ewLanes);
                    break;
                case YELLOW:
                    g.setColor(Color.YELLOW);
                    drawLight(Lane.DEFAULT_LENGTH - 2, Lane.DEFAULT_LENGTH + model.settings.ewLanes);
                    break;
                case GREEN:
                    g.setColor(Color.green);
                    drawLight(Lane.DEFAULT_LENGTH - 3, Lane.DEFAULT_LENGTH + model.settings.ewLanes);
                    break;
            }
            
        }
    }

    //Draws a filled rectangle at the set coordinates of the set size (in grid squares)
    private void fillRect(int x, int y, int wid, int hei){
        g.fillRect(x * sqWidth, y * sqHeight, wid * sqWidth, hei * sqHeight);
    }

    //Draws a traffic light's light at the provided position
    private void drawLight(int x, int y){
        g.fillOval(x * sqWidth + 2, y * sqHeight + 2,sqWidth - 4,sqHeight - 4);
    }

    //Draws a vehicle at the set coordinates of the set size and colour
    private void drawCar(Color c, int x, int y, int wid, int hei)
    {
        g.setColor(c);
        g.fillRect(x * sqWidth + 2, y * sqHeight + 2 , wid * sqWidth - 4, hei * sqHeight - 4);
    }





}
